<template>
  <div
    ref="squareRef"
    class="square-box hw-flex-style-com max-width-p dashboard-detail-card"
    :style="{ backgroundImage: `url(${publicUrls + 'assets/img/squarePageBg.png'})` }"
  >
    <div class="center-boxs">
      <div class="list-heard">
        <div class="list-heard-left">
          {{
            store.styleInfo.id
              ? `已选 : ${templateName} , ` + '请选择要更换的素材'
              : `还未选择${parmas.mode === 1 ? '素材' : '场景'}，选一个试试吧`
          }}
          <span @click="blankLevle" class="doorIcon-parent" v-if="store.styleInfo.id">
            <i class="doorIcon">&#xe66f;</i>
            返回
          </span>
        </div>
      </div>
      <div class="affix-box">
        <van-sticky :container="squareRef" :offset-top="48" @change="stickyChange">
          <div class="typeList-box" :class="{ 'affix-box-sticky': isFixedType }">
            <div
              class="iconList-item"
              @click="setTypeId(item.id)"
              v-for="item in iconList"
              :key="item.id"
              :class="{
                'iconList-item-active': parmas.typeId == item.id,
                'iconList-items': parmas.typeId !== item.id,
              }"
            >
              <span class="iconList-item-name">{{ item.name }}</span>
              <div class="iconList-item-icon">
                <img class="iconList-item-icon1" :src="item.iconActive" alt="" />
                <img class="iconList-item-icon2" :src="item.icon" alt="" />
              </div>
            </div>
          </div>
        </van-sticky>
      </div>
      <div class="center-box-list">
        <t-row :gutter="24">
          <t-col :span="6" v-for="(item, index) in list" :key="item.id" @click="pathInfo(item.id)">
            <div class="pree-item">
              <div class="pree-item-img">
                <img :src="urls + item.coverUrl" fit="cover" position="center" class="free-image-boxs" />
              </div>
              <div class="item-title-box">
                <span class="title1">{{ item.name }}</span>
                <span class="title2"
                  ><img style="width: 16px; height: 16px" :src="publicUrls + 'assets/img/square.svg'" alt="" />
                  {{ item.exhibitTotal > 999 ? '999+' : item.exhibitTotal !== 0 ? item.exhibitTotal : '' }}</span
                >
              </div>
            </div>
          </t-col>
        </t-row>
      </div>
      <div class="square-null" v-if="list.length == 0">
        <img :src="publicUrls + 'assets/img/currentNull.png'" alt="" />
        <span>抱歉,暂无此类素材,<br />即将上线,敬请期待</span>
      </div>
      <t-row v-if="total > 8 && parmas.mode !== 0">
        <t-col :span="12" style="margin-bottom: 0px">
          <div class="hw-flex-style-c-c">
            <t-pagination
              :totalContent="false"
              v-model="parmas.pageNo"
              v-model:pageSize="parmas.pageSize"
              :total="total"
              :showPageSize="false"
              show-jumper
              @page-size-change="onPageSizeChange"
              @current-change="onCurrentChange"
            />
          </div>
        </t-col>
      </t-row>
    </div>
    <div class="info-Shadow" v-if="squareId && infoShow" @click="infoShow = false">
      <i class="doorIcon info-Shadow-close" @click="infoShow = false"> &#xe63d; </i>
      <div class="info-Shadow-content">
        <SquareInfo :id="squareId" @emitStyleInfo="emitStyleInfo" :initMode="initMode" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// @ts-nocheck
// import { getRandomModuleList } from '@/utils/templateEdit';
import { saveConfig } from '@/api/templates/templates';
import { projectDetail } from '@/api/project/project';
import { getUrlTemplateId, pathSquare } from '@/utils/minxin';
import SquareInfo from './squareInfo.vue';
import { publicUrls } from '@/utils/minxin';
import { listcancat } from './index';
import { MessagePlugin } from 'tdesign-vue-next';
import { linkurl } from '@/utils/minxin';
import { urls } from '@/utils/request/upload';
import { doortemplateList, setTemplate, templateInfo } from '@/api/templates/templates';
import { removeLottie } from '@/utils/minxin';
// import { getBanner } from '@/api/banner/banner';
import individualization from './components/individualization.vue';
import { ref, reactive, onActivated } from 'vue';
import { configData } from '@/api/templates/templates';
import { useRouter, useRoute } from 'vue-router';
import { useProjectStore, commonDataStore } from '@/store';

const infoShow = ref(false); //素材详情弹框
const squareRef = ref(null);
const router = useRouter();
const store = useProjectStore();
const commonStore = commonDataStore();
const projectId = ref(null);
const route = useRoute();
const newMode = ref(0);
const loopType = ref(0);
const selectList = ref([]);
const initMode = ref(0);
const squareId = ref(0); //当前点击的素材id
const isFixedType = ref(false); //当前选项是否吸顶
const loading = ref(false);
const bannerList = ref([]);
const list = ref([]);
const templateName = ref(''); //已选素材名称
const iconList = ref([
  {
    name: '全部素材',
    id: 0,
    icon: publicUrls + 'assets/img/doorIcon1.png',
    iconActive: publicUrls + 'assets/img/doorIconactive1.png',
  },
  {
    name: '通用素材',
    id: 423360435204165,
    icon: publicUrls + 'assets/img/doorIcon2.png',
    iconActive: publicUrls + 'assets/img/doorIconactive2.png',
  },
  {
    name: '通史类',
    id: 420206322270277,
    icon: publicUrls + 'assets/img/doorIcon3.png',
    iconActive: publicUrls + 'assets/img/doorIconactive3.png',
  },
  {
    name: '人物生平',
    id: 423360345944133,
    icon: publicUrls + 'assets/img/doorIcon4.png',
    iconActive: publicUrls + 'assets/img/doorIconactive4.png',
  },
  {
    name: '书画类',
    id: 423360488689733,
    icon: publicUrls + 'assets/img/doorIcon5.png',
    iconActive: publicUrls + 'assets/img/doorIconactive5.png',
  },
  {
    name: '革命类',
    id: 423360528699461,
    icon: publicUrls + 'assets/img/doorIcon6.png',
    iconActive: publicUrls + 'assets/img/doorIconactive6.png',
  },
  {
    name: '文物专题',
    id: 423360559190085,
    icon: publicUrls + 'assets/img/doorIcon7.png',
    iconActive: publicUrls + 'assets/img/doorIconactive7.png',
  },
]);

const templateId = ref(0);
const parmas = reactive({
  typeId: 0,
  mode: 2,
  pageNo: 1,
  pageSize: 8,
  value: '',
});
// const
if (route.query.projectId) {
  projectId.value = route.query.projectId;
}
const emitSetMode = (mode: number) => {
  //设置当前的mode值
  parmas.mode = mode;
  init();
};
const initTemplateName = async () => {
  try {
    const url = window.location.href;
    const regex = /\/(\d+)/; // 匹配数字参数的正则表达式
    const match = url.match(regex); // 使用正则表达式匹配数字参数
    let projectId = 0;
    if (match) {
      const numericParam = parseInt(match[1]); // 提取数字参数并将其转换为数字类型

      projectId = Number(numericParam);
    }
    let res = await templateInfo(store.styleInfo.id, projectId);

    templateName.value = res.data.name;
    // list.value=list.value.concat(listcancat)
  } catch {}
};

onActivated(() => {
  if (store.styleInfo.id) {
    initTemplateName();
  }
});
const initInfo = async () => {
  try {
    let res = await projectDetail(localStorage.getItem('projectId'));
    parmas.mode = res.data.mode;
    initMode.value = res.data.mode; 
    newMode.value = res.data.mode; 
    init();
  } catch {}
};
initInfo();
const stickyChange = (isFixed: boolean) => {
  //吸顶状态判断
  // console.log(isFixed);
  isFixedType.value = isFixed;
};
const pathInfo = (id: number) => {
  console.log(id, 'sssssssssssssss');
  squareId.value = id;
  infoShow.value = true;
};
const emitOpenInfo = (id: number) => {
  squareId.value = id;
  infoShow.value = true;
};
const emitStyleInfo = (projectId: number | string, modeType: number) => {
  preview(projectId);
};
const blankLevle = () => {
  router.go(-1);
}; 
const preview = (projectId: number | string) => {
  try {
    // if (route.meta.templateId == 2001 || route.meta.templateId == 2013) return;
    configData(projectId).then((res) => {
      if (res.statusCode == 200) { 
        let arrInfo = res.data;
        // arrInfo.moduleList = getRandomModuleList(arrInfo.moduleList, arrInfo.id, true); //组件生成逻辑
         
         
        store.setstyleInfo(arrInfo,true); 
        arrInfo.backgroundImage = null;
        saveConfig(localStorage.getItem('projectId'), arrInfo);
        store.setTeplateType(0); 
        store.getFreeInfoData(); 
        let templateId: number = commonStore.getDigitalShowroom();
        router.push({
          name: 'templateedit',  
        });
        infoShow.value = false;
      }

      removeLottie();
    });
  } catch {
    removeLottie();
  }
  // console.log('shuaxin');
};
const total = ref(0);

const init = async () => {
  try {
    let { data } = await doortemplateList(parmas);
    total.value = data.total;
    list.value = data.data;

    // list.value=list.value.concat(listcancat)
  } catch {}
};
const setTypeId = (id: number) => {
  parmas.typeId = id;
  parmas.pageNo = 1;
  init();
};
const setTemplateId = async (projectId: number | string) => {
  try {
    let res = await setTemplate(projectId, templateId.value);
  } catch {}
};
const addcruuent = (type: number) => {
  parmas.mode = type;
  init();
};

const onPageSizeChange = (size) => {
  init();
};

const onCurrentChange = (index, pageInfo) => {
  init();
};

// initBanner();
</script>

<style lang="less" scoped>
@import './index.less';

.demo-item {
  position: relative;
  width: 100%;
  height: 320px;
  background: #eeeeee;
  border-radius: 0px;
  .image-boxs-client {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 320px;
    background: #eeeeee;
    border-radius: 0px;
  }
  .image-boxs-box {
    position: relative;
    width: 100%;
    // height: 320px;
    .image-boxs {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      // height: 320px;
      background: #eeeeee;
      border-radius: 0px;
    }
  }
}
.preview-btn-icon {
  margin-right: 8px;
}
.title2 {
  display: flex;
  align-items: center;
  img {
    margin-right: 5px;
  }
}

.center-boxs {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  min-width: 1312px;
  width: 100%;
  height: 100%;
}
.center-box-list {
  display: flex;
  flex-direction: column;
  min-width: 1312px;
  margin: 0 auto;
  width: 100%;
  padding: 0 32px;
  flex: 1;
  overflow-y: auto;
  .t-row {
    width: calc(100% + 24px);
    margin-left: -12px !important;
    padding: 0px !important;
  }
}
.center-box-list::-webkit-scrollbar {
  display: none;
}
.list-heard {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0 32px;
  padding-bottom: 20px;
}
.list-heard-left {
  display: flex;
  align-items: center;
  font-size: 24px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.8);
  line-height: 36px;
}
.doorIcon-parent {
  cursor: pointer;
  margin-left: 16px;
  display: flex;
  align-items: center;
  font-size: 16px;
  line-height: 24px;
  color: #e62b17;
  .doorIcon {
    font-size: 20px;
    margin-right: 4px;
    color: #e62b17;
  }
}
.doorIcon-parent:hover {
  color: #f2573f;
  .doorIcon {
    color: #f2573f;
  }
}
.list-heard-right {
  display: flex;
  align-items: center;
  display: none;
}
.list-heard-right-text1 {
  cursor: pointer;
  height: 28px;
  font-size: 18px;
  font-weight: 400;

  color: rgba(0, 0, 0, 0.59);
  line-height: 28px;
}
.list-heard-right-text1:hover {
  color: #f2573f;
}
.list-heard-right-text1-active {
  height: 28px;
  font-size: 18px;
  font-weight: 500;

  color: #e62b17;
  line-height: 28px;
}
.list-heard-right-text1-active:hover {
  color: #e62b17;
}
.list-heard-right-text2 {
  width: 1px;
  height: 16px;
  background: rgba(0, 0, 0, 0.08);
  margin: 0 24px;
}

.typeList-box {
  width: 100%;
  display: flex;
  padding: 12px 32px;
}
.iconList-item {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  padding-left: 20px;
  padding-right: 16px;

  background: #f5f5f5;
  border-radius: 22px;
  border: 1px solid transparent;
  margin-right: 16px;

  .iconList-item-name {
    font-size: 16px;
    font-weight: 400;
    color: #000000;
    line-height: 24px;
  }
}
.iconList-item-icon {
  position: relative;
  width: 24px;
  height: 24px;
  margin-left: 8px;
}
.iconList-item-icon1 {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  width: 24px;
  height: 24px;
}
.iconList-item-icon2 {
  opacity: 1;
  position: absolute;
  top: 0;
  left: 0;
  width: 24px;
  height: 24px;
}
.iconList-item-active {
  background: #e62b17;
  .iconList-item-icon1 {
    opacity: 1;
  }
  .iconList-item-icon2 {
    opacity: 0;
  }
  .iconList-item-name {
    font-size: 16px;
    font-weight: 400;
    color: #ffffff;
    line-height: 24px;
  }
}
.iconList-items:hover {
  // border: 1px solid rgba(210, 173, 119, 0.3);

  background: #ffece6;
}
//列表样式
.item-img {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  position: relative;
  width: 100%;
  border-radius: 6px;
  .image-boxs-box {
    position: relative;
    width: 100%;
    padding-top: 66.6%;
    border-radius: 6px;
    .image-boxs {
      position: absolute;
      top: 0;
      right: 0;
      transition: all 0.3s;
      min-width: 100%;
      max-width: 100%;
      min-height: 100%;
      max-height: 100%;
      object-fit: cover;
      border-radius: 6px;
      margin: auto;
      display: block;
      width: 0px;
      height: 0px;

      // border: 1px solid rgba(207, 189, 168, 0.3);
    }
  }
}
.remask-box {
  display: flex;
  align-items: center;
  display: none;
  position: absolute;
  border-radius: 4px;
  z-index: 20;
  top: 12px;
  right: 20px;
  height: 24px;
  padding: 0 8px;
  background: rgba(255, 255, 255, 0.5);
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
}
.item-img:hover {
  .image-boxs-box {
    position: relative;
    width: 100%;
    // height: 208px;
    .image-boxs {
      position: absolute;
      top: -8px;
      width: 100%;
      // height: 218px;
      background: #eeeeee;
      border-radius: 8px;
      box-shadow: 0px 4px 10px 0px rgba(217, 217, 217, 0.4);
    }
  }
  // .image-boxs {
  //   width: calc(100% + 16px);
  //   height: 218px;
  //   margin-top: -8px;
  //   margin-left: -5px;
  //   // width: 16px;
  //   // height:10px ;
  //   box-shadow: 0px 4px 10px 0px rgba(185,174,154,0.4);

  // }

  .remask-box {
    display: flex;
  }
}
.item-title-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 4px;
  padding-bottom: 0px;
  .title1 {
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
    line-height: 28px;
  }
  .title2 {
    font-size: 14px;
    font-family: DIN-Regular, DIN;
    font-weight: 400;
    color: #999999;
    line-height: 22px;
  }
}
.square-null {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  margin-bottom: 80px;
  margin-top: 60px;
  img {
    width: 200px;
    height: 200px;
  }
  span {
    font-size: 16px;
    margin-top: 30px;
    color: #f2573f;
    opacity: 0.59;
  }
}
.item-boxs {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin: 0;
}

.t-row {
  width: 100%;
  padding: 0;
  margin-top: 32px;
}
.t-col {
  margin-bottom: 40px;
}

.col-1-box {
  display: flex;
  justify-content: center;
}
.col-2-box {
  display: flex;
  justify-content: center;
}
.col-3-box {
  display: flex;
  justify-content: center;
}
.col-4-box {
  display: flex;
  justify-content: center;
}
.affix-box {
  width: 100%;
}
.affix-box-sticky {
  background: #fff;
}
.info-Shadow {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.4);
  z-index: 1028;
  overflow: scroll;
}
.info-Shadow-content {
  width: 1152px;
  background: #fff;
  margin: 48px auto;
  border-radius: 6px;
}
.info-Shadow-close {
  cursor: pointer;
  position: fixed;
  top: 29px;
  right: 29px;
  z-index: 1040;
  font-size: 24px;
  color: #fff;
}
.hw-flex-style-c-c {
  padding: 0 32px;
}
.pree-item {
  width: 100%;
  height: 296px;
}
.pree-item-img {
  width: 100%;
  height: 260px;
  position: relative;
  overflow: hidden;
  border-radius: 6px;
}
.free-image-boxs {
  cursor: pointer;
  position: absolute;
  top: 0;
  left: 0;
  width: auto;
  height: 260px;
  border-radius: 6px;
}
.pree-item-img:hover {
  .free-image-boxs {
    transition: all 15s;
    left: calc(100%);
    transform: translateX(-100%);
  }
}
:deep(.t-pagination__number:active) {
  border-color: #e62b17;
  color: #e62b17;
}
</style>
